package com.guanyong.services;

import com.guanyong.dao.IPriceTagDaoMapper;
import com.guanyong.model.PriceTagModel;

import com.guanyong.utils.Cache.ICahce.ICache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created by mxk on 2017/5/31.
 * 价格业务
 */
@Service
public class PriceTagService {
    @Autowired
    IPriceTagDaoMapper iPriceTagDaoMapper;

    @Autowired
    ICache cache;

    @Value("${ad.pricetag.cachekey}")
    String pricetagcahekey;

    public List<PriceTagModel> GetAll(){
        List<PriceTagModel> list=getFromCache();
        if (list==null || list.size()==0){
            list=iPriceTagDaoMapper.selectAll();
            cache.put(pricetagcahekey,"list",list);
        }
        return  list;

    }

    public PriceTagModel GetPriceTagByCache(String pricetagid){
        PriceTagModel priceTagModel=null;
        List<PriceTagModel> list=GetAll();
        for (PriceTagModel priceTagModel1 :list){

            if (pricetagid.equals(priceTagModel1.getfId())){
                priceTagModel=priceTagModel1;
                break;
            }
        }

        return  priceTagModel;
    }


    List<PriceTagModel>  getFromCache(){
        List<PriceTagModel> list=null;
        Object model = cache.get(pricetagcahekey, "list");
        try{
            if (model != null) {
                list = (List<PriceTagModel>) model;
            }
        }catch (Exception ex){

        }
        return list;
    }

    public HashMap<String,Object> GetPriceTagList(
                                              String name,
                                              Integer pno,
                                              Integer pagesize,
                                              Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iPriceTagDaoMapper
                    .selectPriceTagCount(name);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<PriceTagModel> priceTagModelList
                =iPriceTagDaoMapper.selectPriceTagList(name,(pno-1)*pagesize,
                pagesize);
        hashMap.put("list",priceTagModelList);
        return hashMap;
    }


    public PriceTagModel GetPriceTagById(String fid){
        return iPriceTagDaoMapper.selectByPrimaryKey(fid);

    }

    public Integer AddPriceTag(PriceTagModel priceTagModel){
        Integer res=  iPriceTagDaoMapper.insert(priceTagModel);
        if (res >-1){
            List<PriceTagModel> list=iPriceTagDaoMapper.selectAll();
            cache.put(pricetagcahekey,"list",list);
        }
        return res;
    }

    public Integer UpdatePriceTag(PriceTagModel priceTagModel){
        Integer res = iPriceTagDaoMapper.update(priceTagModel);
        if (res >-1){
            List<PriceTagModel> list=iPriceTagDaoMapper.selectAll();
            cache.put(pricetagcahekey,"list",list);
        }
        return res;
    }
}
